<?php

/**
 * @author chenshubo
 * Unibuy 供应商（轻奢）
 */
namespace App\Service\OrderService\OrderRouteService;
use App\Service\Code\Normal;
use Service\AppService\IdService;
use App\Model\PssModel\OrderSupplier;
use App\Service\OrderService\BaseSupplierRouterService;
use App\Service\VendorClient\ShopubuyApi\ShopubuyApiClient;

class Shopubuy extends BaseSupplierRouterService
{
    /**
     * 创建订单
     * @param array $param
     * @return mixed
     */
    public function createOrder( $param = [] )
    {
        $orderItems = [];
        $supplierGoodsInfo  = $param['supplierGoodsInfo'];
        $userGoodsList      = $supplierGoodsInfo['userGoodsList'];
        $goodsList          = $supplierGoodsInfo['goodsList'];
        $supplierCode       = $supplierGoodsInfo['supplierCode'];
        $currentGoods       = current($goodsList);
        $supplierCommodityType = $currentGoods['supplierCommodityType'];
        
        //下游是否申报
        $isDelare = (
            (isset($param['order']['isOrderDeclare']) && $param['order']['isOrderDeclare'] == 1) ||
            (isset($param['order']['isPayDeclare']) && $param['order']['isPayDeclare'] == 1)
            ) ? true : false;
        
        //是否云仓商城
        $isShop = isset($param['order']['isSplitOrder']) && $param['order']['isSplitOrder'] == 1 ? true : false ;
            
        $param['order']['supplier_psn'] = OrderSupplier::instance()->getCreateOrderSn($supplierCode);
        $param['order']['supplier_csn'] = IdService::instance()->getOtherID();
        
        if( $isDelare && !$isShop && $supplierCommodityType != GOODS_TYPE_YBMY ) {
            $param['order']['supplier_dsn'] = (string)$param['order']['orderSn'];
        } else {
            $param['order']['supplier_dsn'] = (string)IdService::instance()->getOtherID();
        }
        $param['order']['isQueryExpress'] = 1;
        $res = OrderSupplier::instance()->getSupplierCreateOrderData($param);
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        
        $orderItems[] = $res['data'];
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'supplierCode' => $supplierCode,
            'orderItems' => $orderItems
        ]);
    }
    
    /**
     * 订单支付
     * @param array $param
     * @return mixed
     */
    public function orderPay( $param = [] )
    {
        return $this->callSdkApi(ShopubuyApiClient::class,$param['apiBody'])->createOrder($param);
    }
    
    /**
     * 订单取消
     * @param array $param
     * @return mixed
     */
    public function orderCancel( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }
    
    /**
     * 订单售后
     * @param array $param
     * @return mixed
     */
    public function orderAfterSale( $param = [] )
    {
        return $this->callSdkApi(ShopubuyApiClient::class,$param['apiBody'])->orderCancel($param);
    }
    
    /**
     * 查询订单售后
     * @param array $param
     * @return mixed
     */
    public function queryOrderAfterSale( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }
    
    /**
     * 获取物流信息
     * @param array $param
     */
    public function queryOrderExpress( $param = [] )
    {
        $queryBody = $param['queryBody'];
        $apiBody   = $param['apiBody'];
        $orderID   = $queryBody['orderID'];
        $orderInfo = OrderSupplier::instance()->info(['id'=>$orderID,'company_id'=>$queryBody['supplierID']]);
        if( empty($orderInfo) ) {
            return Normal::returnCode(Normal::FAIL,"订单信息不存在，请核对！");
        }
        
        $res = $this->callSdkApi(ShopubuyApiClient::class,$apiBody)->{__FUNCTION__}($orderInfo);
        if( $res['code'] != Normal::SUC ) {
            return Normal::returnCode(Normal::FAIL,$res['msg']);
        }
        if( empty($res['data']['list'])) {
            return Normal::returnCode(Normal::FAIL,"还未发货");
        }
        
        $queryOrder = current($res['data']['list']);
        
        
        
        //待发货状态
        if( $queryOrder['orderState'] == 30 ) {
            return Normal::returnCode(Normal::FAIL,"还未发货[100000]");
        }
         
        //不满足已发货状态
        if( !in_array($queryOrder['orderState'],['55','65']) ) {
            //平台订单状态  100：已退款，102：已取消，需要自动解绑供应商,并且是未售后的情况下，需要自动解绑供应商
            if( $orderInfo['after_sale_status'] == 0 ) {
                OrderSupplier::instance()->where('id',$orderInfo['ID'])->update([
                    'order_push_status' => 0,
                    'order_push_remark' => '下游未发起售后的情况下，云仓自动解绑供应商订单！',
                ]);
            }
        }
        
        //判断logisticsInfoDtoList是否为空
        if( empty($queryOrder['logisticsInfoDtoList']) ) {
            return Normal::returnCode(Normal::FAIL,"还未发货[100001]");
        }
        
        $logisticsInfoDtoList = current($queryOrder['logisticsInfoDtoList']);
        return Normal::returnCode(Normal::SUC,"已发货",[
            'expressInfo' => [
                'express_name'  => $logisticsInfoDtoList['logisticsName'],
                'express_sn'    => $logisticsInfoDtoList['logisticsNo'],
            ]
        ]);
    }
    
    /**
     * 商品列表查询接口
     * @param array $param
     * @return mixed
     */
    public function queryGoodsList( $param = [] )
    {
        $res = $this->callSdkApi(ShopubuyApiClient::class,$param['apiBody'])->{__FUNCTION__}($param['queryBody']);
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'total' => $res['data']['total'],
            'list'  => $res['data']['list']
        ]);
    }

    /**
     * 店铺列表查询接口
     * @param array $param
     * @return mixed
     */
    public function queryShopList( $param = [] )
    {
        $res = $this->callSdkApi(ShopubuyApiClient::class,$param['apiBody'])->{__FUNCTION__}($param['queryBody']);
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        return Normal::returnCode(Normal::SUC,"获取成功",$res);
    }
    
    /**
     * 商品详情查询接口
     * @param array $param
     * @return mixed
     */
    public function queryGoodsDetail( $param = [] )
    {
        return $this->callSdkApi(ShopubuyApiClient::class,$param['apiBody'])->{__FUNCTION__}($param['queryBody']);
    }
}